<?php

namespace App\Console\Commands;

use App\Model\OrderError;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class checkError extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'checkError';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                $res = OrderError::get();
                if(!$res -> isEmpty()){
                    foreach ($res as $v){
                        if(time() - $v['time'] > 600){
                            Mail::raw('出现需要手动回调的订单,订单id:'.$v['bid'].'订单号:'.$v['orderno'], function($message){
                                $message->to('82779539@qq.com');
                                $message->to('171161005@qq.com');
                                $message->to('499841540@qq.com');
                                $message->to('376808741@qq.com');
                                $message->subject('充值成功超过100元就手动推送成功');
                            });
                        }
                    }
                }
            }catch (\Exception $e){
                echo $e->getMessage();
            }
            try {
                $token = '738e1b71-3084-4851-8ff1-b1c091fc1a7e';
                $res = $this -> curlGet('http://openapi.rytjsj.com/business-center/business/getInfo','get',$token);
                $amount = intval(json_decode($res) -> datas -> values -> amount);
                if($amount < 10000){
                    Mail::raw('软游商户余额为:'.$amount, function($message){
                        $message->to('82779539@qq.com');
                        $message->to('171161005@qq.com');
                        $message->to('499841540@qq.com');
                        $message->to('376808741@qq.com');
                        $message->subject('软游商户余额不足1W，速度充值');
                    });
                }
            }catch (\Exception $e){
                echo $e->getMessage();
            }
//            try {
//                $token = '738e1b71-3084-4851-8ff1-b1c091fc1a7e';
//                $res = $this -> curlGet('http://openapi.rytjsj.com/business-center/business/getInfo','get',$token);
//                $amount = intval(json_decode($res) -> datas -> values -> amount);
//                if($amount < 10000){
//                    Mail::raw('软游商户余额为:'.$amount, function($message){
//                        $message->to('82779539@qq.com');
//                        $message->to('171161005@qq.com');
//                        $message->to('499841540@qq.com');
//                        $message->to('376808741@qq.com');
//                        $message->subject('软游商户余额不足1W，速度充值');
//                    });
//                }
//            }catch (\Exception $e){
//                echo $e->getMessage();
//            }
            sleep(300);
        }

    }

    public function curlGet($url,$method,$token)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Referer: http://120.78.210.213:83/',
                'token: '.$token
//                'token: 738e1b71-3084-4851-8ff1-b1c091fc1a7e'
            )
        );
        if ($method == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);

            curl_setopt($ch, CURLOPT_POSTFIELDS, 0);
        } elseif ($method == 'get') {
            curl_setopt($ch, CURLOPT_HEADER, 0);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
//        curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
//        curl_setopt($ch, CURLOPT_PROXYPORT, "2802");
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
}
